﻿//var bookingModel = function (booking, groupBy) {
//    this.selected = ko.observable(false);

//    this.lastname = ko.observable(booking.LastName);
//    this.firstname = ko.observable(booking.FirstName);
//    this.department = ko.observable(booking.Department);
//    this.fromTime = ko.observable(booking.FromTime);
//    this.toTime = ko.observable(booking.ToTime);
//    this.fromDate = ko.observable(booking.FromDate);
//    this.toDate = ko.observable(booking.ToDate);
//    this.destination = ko.observable(booking.Destination);
//    this.purpose = ko.observable(booking.Purpose);
//    this.requestTime = ko.observable(booking.RequestTime);
//    this.requestDate = ko.observable(booking.RequestDate);
//    this.remark = ko.observable(booking.Remark);

//    this.status = ko.observable(booking.Status);
//    this.groupBy = ko.observable(booking.GroupBy);
//}

var bookingImplement = new Object();

bookingImplement.getBooking = function (bookingId) {
    var URL = "/Handler/MyService.asmx/GetBooking";
    var bookObject;
    $.ajax({
        type: "POST",
        url: URL,
        data: JSON.stringify({ bookingId: bookingId }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        success: function (data) {
            bookObject = data.d;
        },
        error: function () {
            bookObject = null;
        }
    });

    return bookObject;
}

bookingImplement.updateBooking = function (booking) {
    var URL = "/Handler/MyService.asmx/UpdateBooking";
    var result;
    $.ajax({
        type: "POST",
        url: URL,
        data: JSON.stringify({ booking: booking }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        success: function (data) {
            result = data.d;
        },
        error: function () {
            result = false;
        }
    });

    return result;
}

bookingImplement.getBookings = function (status, pageNumber, keyword, fromDate, toDate) {
    var URL = "/Handler/MyService.asmx/GetBookings";
    var bookObject;
    $.ajax({
        type: "POST",
        url: URL,
        data: JSON.stringify({ status: status, pageNumber: pageNumber, keyword: keyword }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        success: function (data) {
            bookObject = data.d;
        },
        error: function () {
            bookObject = null;
        }
    });

    return bookObject;
}

bookingImplement.getBookingList = function (status, pageNumber, keyword, fromDate, toDate) {
    var URL = "/Handler/MyService.asmx/GetBookingList";
    var bookObject;
    $.ajax({
        type: "POST",
        url: URL,
        data: JSON.stringify({ status: status, pageNumber: pageNumber, keyword: keyword }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        success: function (data) {
            bookObject = data.d;
        },
        error: function () {
            bookObject = null;
        }
    });

    return bookObject;
}

bookingImplement.deleteBookings = function (bookingIds, status) {
    var URL = "/Handler/MyService.asmx/";

    if (status === 'deleted')
        URL += 'DestroyBookings';
    else
        URL += 'DeleteBookings';
    var result;
    $.ajax({
        type: "POST",
        url: URL,
        data: JSON.stringify({ bookingIds: bookingIds }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        success: function (data) {
            result = data.d;
        },
        error: function () {
            result = false;
        }
    });

    return result;
}

bookingImplement.undoBookings = function (bookingIds) {
    var URL = "/Handler/MyService.asmx/UndoBookings";
    var result;
    $.ajax({
        type: "POST",
        url: URL,
        data: JSON.stringify({ bookingIds: bookingIds }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        success: function (data) {
            result = data.d;
        },
        error: function () {
            result = false;
        }
    });

    return result;
}


bookingImplement.getTabs = function () {
    var URL = '/Get.asmx/GetTabs';
    var tabs;
    $.ajax({
        type: "POST",
        url: URL,
        async: false,
        success: function (data) {
            tabs = data.d;
        },
        error: function () {
        }
    });
    return tabs;
}
